<?php

class Tutor {

	function __construct() {}
	
	// All function about db such as insert/edit/delete
	function updateMarkAndFeedback($stuid,$testid,$questionid,$attempt,$mark,$feeback){
		global $db;
		$sql = "update testanswers set Mark = ".$mark." , Feedback = '".$feeback."' where TestID = ".$testid." and QuestionNo = "
				.$questionid." and StudentID = ".$stuid." and Attempt = ".$attempt;
				
		//echo $sql;
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;	
	}
	function setOverallMark($stuid,$testid,$attempt,$overall_mark){
		global $db;
		$sql = "update testattempts set OverallMark = ".$overall_mark." where TestID = ".$testid." and StudentID = ".$stuid." and Attempt = ".$attempt;
		//echo $sql;
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;	
	}
	function setFeedbackStatus($stuid,$testid,$attempt){
		global $db;
		$sql = "UPDATE testattempts set FeedbackStatus = 1 where TestID = ".$testid." and StudentID = ".$stuid." and Attempt = ".$attempt;
		//echo $sql;
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;	
	}
	function getStudentAttemps($p) {
		global $db;
		if($p == 1) {
			$sql = "SELECT Students.StudentID as StudentID, t1.TestID  as  TestID, Year,  Title, Deadline, TestStatus, OverallMark, FeedbackStatus 
				FROM Students, Tests, TestAttempts t1 where Students.StudentID = t1.StudentID AND Tests.UnitID IS NOT NULL and Tests.TestID = t1.TestID and t1.Attempt 
				IN (select max(t2.Attempt) from TestAttempts t2 where t2.TestID = t1.TestID and t2.StudentID = t1.StudentID)		
			";
		} elseif($p == 2) {
			$sql = "SELECT Students.StudentID as StudentID, t1.TestID  as  TestID, Year,  Title, Deadline, TestStatus, OverallMark, FeedbackStatus 
				FROM Students, Tests, TestAttempts t1 where Students.StudentID = t1.StudentID AND Tests.UnitID IS NULL and Tests.TestID = t1.TestID and t1.Attempt 
				IN (select max(t2.Attempt) from TestAttempts t2 where t2.TestID = t1.TestID and t2.StudentID = t1.StudentID)		
			";
		}
		/*$sql = "SELECT Students.StudentID as StudentID, t1.TestID  as  TestID, Year,  Title, Deadline, TestStatus, OverallMark, FeedbackStatus 
				FROM Students, Tests, TestAttempts t1 where Students.StudentID = t1.StudentID and Tests.TestID = t1.TestID and t1.Attempt 
				IN (select max(t2.Attempt) from TestAttempts t2 where t2.TestID = t1.TestID and t2.StudentID = t1.StudentID)		
		";*/
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}

	function showAllReadyTests($p) {
		global $db;
		if($p == 1) {
			$sql = "SELECT * FROM tests WHERE Status=2 AND UnitID IS NOT NULL";	
		} elseif($p == 2) {
			$sql = "SELECT * FROM tests WHERE Status=2 AND UnitID IS NULL";
		}
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}

	function showAllNReadyTests($p) {
		global $db;
		if($p == 1) {
			$sql = "SELECT * FROM tests WHERE Status=1 AND UnitID IS NOT NULL";	
		} elseif($p == 2) {
			$sql = "SELECT * FROM tests WHERE Status=1 AND UnitID IS NULL";
		}
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}

	function doTestReady($testid) {
		global $db;
		$sql = "UPDATE tests SET Status=2 WHERE TestID=$testid";
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}

	function showAllReadyTestsInUnit() {
		global $db;
		$sql = "SELECT * FROM tests WHERE Status=2 AND UnitID IS NOT NULL";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function showAllReadyTestsInUnitID($id) {
		global $db;
		$sql = "SELECT * FROM tests WHERE Status=2 AND UnitID=$id";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}

	function showAllReadyTestsInModule() {
		global $db;
		$sql = "SELECT * FROM tests WHERE Status=2 AND UnitID IS NULL";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}

}

?>